//
//  SkillTabVC.m
//  EVETracker
//
//  Created by Scott Guyer on 7/30/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "SkillVC.h"
#import "SkillListVC.h"
#import "SkillGroupVC.h"
#import "SkillTabVC.h"
#import "InventoryItem.h"
#import "Skill.h"
#import "EVEdb.h"
#import "defs.h"


@implementation SkillTabVC

@synthesize navVC, rootVC, searchBar, searchButton, searchTypeSegControl;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
	if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) 
	{
		listVC = nil;
		groupVC = nil;
		
		fullData = nil;
		savedData = [[NSMutableDictionary alloc] initWithCapacity:64];
		filteredData = [[NSMutableDictionary alloc] initWithCapacity:64];
		
		self.tabBarItem.title = [NSString stringWithString:@"Skills"];
		self.tabBarItem.image = [UIImage imageNamed:@"book.png"];
	}

	return self;
}


- (void) showBrowseView
{
	if ( groupVC == nil )
	{
		groupVC = [[SkillGroupVC alloc] initWithNibName:@"SkillGroupView" bundle:nil];
	}
	
	if ( [listVC.view superview] != nil )
		[listVC.view removeFromSuperview];
	if ( [searchBar superview] != nil )
		[searchBar removeFromSuperview];
	rootVC.navigationItem.titleView = nil;
	
	
	[groupVC viewWillAppear:NO];
	
	// TODO: first time through, this frame is WAY wrong.  WTH?
	CGRect f = rootVC.view.frame;
	f.origin.y = 0;
	groupVC.view.frame = f;
	groupVC.navVC = navVC;
	[rootVC.view addSubview:groupVC.view];
	
	rootVC.navigationItem.title = @"Skill Groups";
	
	[groupVC viewDidAppear:NO];
	
	[groupVC setBrowserMode];
}


- (void) showSearchView
{
	if ( listVC == nil )
	{
		listVC = [[SkillListVC alloc] initWithNibName:@"SkillListView" bundle:nil];
	}
	
	if ( fullData == nil )
	{
		fullData = [EVEdb getAllSkills];
		[filteredData addEntriesFromDictionary:fullData];
	}
	
	if ( [groupVC.view superview] != nil )
		[groupVC.view removeFromSuperview];
	
	[listVC viewWillAppear:NO];
	
	CGRect f = rootVC.view.frame;
	
	CGRect sf = searchBar.frame;
	sf.origin.x = 0;
	searchBar.frame = sf;
	
	CGRect lf = CGRectMake( 0, sf.size.height, f.size.width,
						   (f.size.height - sf.size.height));
	listVC.view.frame = lf;
	listVC.navVC = navVC;
	
	searchTypeSegControl.tintColor = [UIColor darkGrayColor];
	rootVC.navigationItem.titleView = searchTypeSegControl;
	
	[listVC updateViewWithData:filteredData];
	
	[rootVC.view addSubview:searchBar];
	[rootVC.view addSubview:listVC.view];
	
	rootVC.navigationItem.title = @"Skill List";
	
	[listVC viewDidAppear:NO];
}


- (void)viewDidLoad 
{
	NSLog(@"%s", __FUNCTION__);
	
	CGRect f = self.view.frame;
	navVC.view.frame = f;
	[self.view addSubview:navVC.view];
	
//	[self showBrowseView];	
//	viewMode = SkillTabViewModeBrowse;
}


- (IBAction) searchButtonAction:(id)sender 
{
	NSLog(@"%s", __FUNCTION__);
	if ( viewMode == SkillTabViewModeBrowse )
	{
		[self showSearchView];
		viewMode = SkillTabViewModeSearch;
	}
	else
	{
		[self showBrowseView];
		viewMode = SkillTabViewModeBrowse;
	}
}


- (IBAction) searchTypeAction:(id)sender 
{
	[searchBar resignFirstResponder];
	if ( searchTypeSegControl.selectedSegmentIndex == 0 )
	{
		[listVC updateViewWithData:filteredData];
		searchBar.text = filter;
	}
	else
	{
		[listVC updateViewWithData:nil];
		searchBar.text = @"";
	}
}


#pragma mark UISearchBarDelegate

- (void)searchBarTextDidBeginEditing:(UISearchBar *)sb
{
	NSLog(@"%s", __FUNCTION__);
	
	sb.showsCancelButton = YES;
	
	if ( searchTypeSegControl.selectedSegmentIndex == 0 )
	{
		[savedData removeAllObjects];
		[savedData addEntriesFromDictionary:filteredData];
	}
}       


- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
	if ( searchTypeSegControl.selectedSegmentIndex == 0 )
	{
		[filteredData removeAllObjects]; // clear the filtered array first	
		
		NSString *elem;
		for (elem in fullData)
		{
			NSComparisonResult result = [elem compare:searchText options:NSCaseInsensitiveSearch
												range:NSMakeRange(0, [searchText length])];
			if (result == NSOrderedSame)
			{
				[filteredData setObject:(NSNumber*)[fullData objectForKey:elem] forKey:elem];
			}
		}
		
		if ( filter != nil )
			[filter release];
		filter = [searchText copy];
		
		[listVC updateViewWithData:filteredData];
	}
	
}


- (void)searchBarCancelButtonClicked:(UISearchBar *)sb
{
	NSLog(@"%s", __FUNCTION__);
	
	if ( searchTypeSegControl.selectedSegmentIndex == 0 )
	{
		if ( sb.text.length > 0 )
		{
			[filteredData removeAllObjects];
			if ( (savedData == nil) || ([savedData count] <= 0 ) )
				[filteredData addEntriesFromDictionary:fullData];
			else
				[filteredData addEntriesFromDictionary:savedData];
		}
	}
	
	[sb resignFirstResponder];
	sb.text = filter;
	
	
	[listVC updateViewWithData:filteredData];
}


- (void)searchBarSearchButtonClicked:(UISearchBar *)sb
{
	[sb resignFirstResponder];
	
	if ( searchTypeSegControl.selectedSegmentIndex == 1 )
	{
		NSString* searchString = searchBar.text;
		if ( [searchString length] <= 0 )
			return;
		
		NSDictionary* results = [EVEdb findItemsWithPhrase:searchBar.text];
		if ( results != nil )
		{
			if ( [results count] > 0 )
				[listVC updateViewWithData:results];
			
			[results release];
		}
	}
}

			
- (void)searchBarTextDidEndEditing:(UISearchBar *)sb
{
	NSLog(@"%s", __FUNCTION__);
	sb.showsCancelButton = NO;
}




- (void)viewWillAppear:(BOOL)animated
{
	CGRect f = rootVC.view.frame;
	f.size.height = 367;
	[rootVC.view setFrame:f];
	
	if ( (listVC == nil) && (groupVC == nil) )
	{
		if ( viewMode == SkillTabViewModeBrowse )
		{
			[self showBrowseView];
		}
		else
		{
			[self showSearchView];
		}
	}	
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations
	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void) jenny 
{
	NSLog(@"%s", __FUNCTION__);
	
	[listVC jenny];
	WIPE( listVC)
	[groupVC jenny];
	WIPE( groupVC)
	
	WIPE( fullData)
	WIPE( filter)
	
	[savedData removeAllObjects];
	[filteredData removeAllObjects];
}

- (void) didReceiveMemoryWarning
{
	NSLog(@"%s", __FUNCTION__);
}


- (void)dealloc {
	NSLog(@"%s 0x%x", __FUNCTION__, self);
	
	WIPE( navVC)
	WIPE( rootVC)
	WIPE( searchButton)
	WIPE( searchBar)
	WIPE( searchTypeSegControl)
	WIPE( listVC)
	WIPE( groupVC)
	WIPE( fullData)
	WIPE( savedData)
	WIPE( filteredData)
	WIPE( filter)	
	
	[super dealloc];
}


@end
